﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Trains.Common.FileHelper;
using Trains.Models.Interfaces;

namespace Trains.Models
{
    public class StationRepository: IStationRepository
    {
        public List<Station> Stations { get; set; } = new List<Station>();
        
        /// <summary>
        /// Get all stations
        /// </summary>
        /// <returns></returns>
        public List<Station> GetAllStations()
        {            
            return this.Stations;
        }

        /// <summary>
        /// get station via name
        /// </summary>
        /// <param name="name">station name</param>
        /// <returns></returns>
        public Station GetStationByName(string name)
        {
            return this.Stations.FirstOrDefault(f => string.Equals(f.Name, name, StringComparison.CurrentCultureIgnoreCase));
        }

        /// <summary>
        /// add a station into list
        /// </summary>
        /// <param name="stationName">station name</param>
        /// <returns>new added obj</returns>
        public Station AddStation(string stationName)
        {
            var station = new Station(stationName);            
            this.Stations.Add(station);
            return station;
        }

        /// <summary>
        /// handle station data into list
        /// </summary>
        /// <param name="routes">a list like {AB5, BC4}</param>
        public List<Station> HandleStation()
        {
            try
            {
                var routes = FileHelper.LoadFile();
                var routeList = routes.Split(',').ToList();
                foreach (var route in routeList)
                {
                    var fromStationName = route.Substring(0, 1);
                    var toStationName = route.Substring(1, 1);
                    var distance = Convert.ToInt32(route.Substring(2));

                    //check if already has from station
                    var fromStation = GetStationByName(fromStationName);
                    if (fromStation == null)
                    {
                        fromStation = AddStation(fromStationName);
                    }

                    //check if already has to station
                    var toStation = GetStationByName(toStationName);
                    if (toStation == null)
                    {
                        toStation = AddStation(toStationName);
                    }

                    //check if already exsit route
                    var exsitRoute = fromStation.GetRouteOfStation(toStationName);
                    if (exsitRoute == null)
                    {
                        fromStation.AddRoute(new Route(fromStation, toStation, distance));
                    }
                }

                return this.Stations;
            }
            catch(Exception e)
            {
                throw new Exception("The format of data resource is wrong.");
            }
        }
    }
}
